package reduce.fz.com.reduce.util.photo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import java.io.File;
import java.util.Random;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.util.ImageSaveUtil;
import reduce.fz.com.reduce.util.base.CropParam;
import reduce.fz.com.reduce.util.base.FileCache;

/**
 * Created by qingshengzheng on 15/11/9.
 */
public class PhotoTake {
    private Context mContext;
    private Activity mActivity;
    //拍照
    public final static int TAKE_PIC = 665;
    //截图
    public final static int CROP_PIC=666;


    public PhotoTake(Activity activity) {
        this.mContext = activity.getBaseContext();
        this.mActivity=mActivity;
    }

    /**
     * 初始化phototake
     *
     * @return
     */
    public static PhotoTake instance(Activity mActivity) {
        PhotoTake take = new PhotoTake(mActivity);
        return take;
    }

    /**
     * 执行拍照操作
     */
    public  Uri takePhoto() {
        Uri imageUri = null;
        //执行拍照前，应该先判断SD卡是否存在
        String SDState = Environment.getExternalStorageState();
        if (SDState.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            String savePath = ImageSaveUtil.getImageSavePath(mContext);
            String avatarName = System.currentTimeMillis() + "_" + new Random().nextInt(1000) + ".jpg";
            imageUri = Uri.fromFile(new File(savePath, avatarName));
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
            mActivity.startActivityForResult(intent, TAKE_PIC);
        } else {
            Toast.makeText(mContext, R.string.sd_not_found, Toast.LENGTH_LONG).show();
        }
        return imageUri;
    }

    /**
     * 执行压缩截取操作
     */
    public Uri cropImageUri( Uri imageUri, CropParam param) {
        Uri oriUri = imageUri;
        String avatarName = System.currentTimeMillis() + "_" + new Random().nextInt(1000) + ".jpg";
        imageUri = Uri.fromFile(new File(FileCache.getCropDir(mContext), avatarName));
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(oriUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", param.aspectX);
        intent.putExtra("aspectY", param.aspectY);
        intent.putExtra("outputX", param.outputX);
        intent.putExtra("outputY", param.outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        mActivity.startActivityForResult(intent, CROP_PIC);
        return imageUri;
    }

    /**
     * 广播图片到相册
     * @param imageUri
     */
    public void photoBroadCast(Uri imageUri) {
        Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imageUri);
        mActivity.sendBroadcast(localIntent);
    }


}
